//JQuery extensions for accounting functions
$.extend({
	getUrlVars: function() { //Get all request parameters
		var vars = [], hash;
	    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	    for(var i = 0; i < hashes.length; i++)
	    {
	      hash = hashes[i].split('=');
	      vars.push(hash[0]);
	      vars[hash[0]] = hash[1];
	    }
	    return vars;
	},
	getUrlVar: function(name) { //Get specific request parameter value 
		return $.getUrlVars()[name];
	},
	facebookLogin: function () { // Login with facebook
		var facebookAuthUrl = $fbAuthUrlRaw;
		facebookAuthUrl = facebookAuthUrl.replace("FB_APP_ID",$fbAppId);
		facebookAuthUrl = facebookAuthUrl.replace("FB_REDIRECT_URI",$fbredirectUri);
		window.location.href = facebookAuthUrl;
	},
	initEmber: function() { //Initializes ember objects, controllers and views
		$.createObjectClasses();
		$.createControllers();
	},
	loadMasterData: function () { //loads master data from database
		var param = Accounting.ajaxParamController.newParam();
		$.ajaxService("loadmasterdata",param,function(data,status,jqXHR) {
			Accounting.accountController.loadAccounts(data.accounts);
		});
	},
	showError: function(msg) {
		$("#msg-text").html(msg)
						.removeClass("success")
						.addClass("error");
		$("#message").fadeIn();
	},
	showSuccess: function(msg) {
		$("#msg-text").html(msg)
						.removeClass("error")
						.addClass("success");
		$("#message").fadeIn();
	},
	closeMessage: function() {
		$("#message").fadeOut();
	}
});

//Functions to be done after the document is ready
$(document).ready(function() {
	//Authenticate against facebook before initializing anything else
	/**(($fbCode = $.getUrlVar("code")) == null) ? $.facebookLogin() : (function() {
		//Initialize the Ember objects
		$.initEmber();
		//Load the master data
		$.loadMasterData();
	}).call();**/
	//Initialize the Ember objects
	$.initEmber();
	//Load the master data
	$.loadMasterData();
});
